<?php
/**
*
*===================================================================
*
*  phpBB Code Repository -- Installer File
*-------------------------------------------------------------------
*	Script info:
* Version:		2.0.0 - "Xerxes" - Alpha 1
* SVN ID:		$Id: install_install.php 3 2009-07-31 02:25:51Z sTraTosphere.programming $
* Copyright:	(c) 2008, 2009 | Obsidian
* Copyright:	(c) 2008, 2009 | Deadpool (original author) -- http://the-webmasters-resource.info
* License:		http://opensource.org/licenses/gpl-2.0.php | GNU Public License v2
* Package:		Install
*
*===================================================================
*
*/

/**
* @ignore
*/
if (!defined('IN_INSTALL'))
{
	exit;
}

if (isset($config['crs_version']))
{
	// if this value exists it /is/ already installed... so we don't need this module
	return;
}

if (!empty($setmodules))
{
	$module[] = array(
		'module_type'		=> 'install',
		'module_title'		=> 'INSTALL',
		'module_filename'	=> substr(basename(__FILE__), 0, -strlen(PHP_EXT)-1),
		'module_order'		=> 10,
		'module_subs'		=> '',
		'module_stages'		=> array('MAIN', 'DATABASE', 'FINAL'),
		'module_reqs'		=> ''
	);
}

/**
* Installation
* @package install
*/
class install_install extends module
{
	public function __construct(&$p_master)
	{
		$this->p_master = &$p_master;
	}

	public function main($mode, $sub)
	{
		global $user, $template, $phpbb_root_path, $phpEx;

		switch ($sub)
		{
			case 'main':
				$this->crs_install_main();
				$this->tpl_name = 'install_accept';
			break;

			case 'database':
				$this->crs_db_installer();	
				$this->tpl_name = 'install_body';
			break;

			case 'final':
				$this->page_title = $user->lang['STAGE_FINAL'];
				$this->tpl_name = 'install_body';

				set_config('crs_version', CRS_VERSION);
				set_config('crs_enabled', 0); //Defaults to disabled so admin can go setup perms first without n00bs whining.
				set_config('crs_source_path', 'projects');
				set_config('crs_dump_geshi_css', 0);
				
				set_config('crs_allow_project_notify', 0);
				set_config('crs_packs_path', 'store/packs');
				set_config('crs_livepack_max_age', 2);
				set_config('crs_package_extension', 0);
				set_config('crs_project_index_limit', 7);
				set_config('crs_livepack_skip_cache', 0);
				
				// Global enable/disable of features/modules.
				set_config('crs_use_code_viewer', 1);
				set_config('crs_use_rss', 1);
				set_config('crs_use_comments', 1);
				set_config('crs_use_download', 1);
				set_config('crs_use_update_subscribe', 1);
				set_config('crs_use_livepack', 1);
				//set_config('crs_use_tracker', 1);
				//set_config('crs_use_tasks', 1);
		

				$template->assign_vars(array(
					'TITLE'			=> $user->lang['STAGE_FINAL'],
					'BODY'			=> $user->lang['INSTALL_FINAL_BODY'],
					'L_SUBMIT'		=> $user->lang['VIEW_CRS'],
					'U_ACTION'		=> append_sid(PHPBB_ROOT_PATH . 'code.' . PHP_EXT),
				));
			break;
		}
	}

	private function crs_db_installer()
	{
		global $user, $template, $mode, $config;

		// If this config value exists we /must/ have installed it *hopefully* so no need to run it again...
		if (!isset($config['crs_version']))
		{
			$install_path = INSTALLER_PATH . 'schemas/';
		//! Load in the schema and execute.  This may get bloody.  @_@;;
			load_schema($install_path, false);

			// Setup $auth_admin class so we can add permission options
			if(!class_exists('auth_admin'))
			{
				include(PHPBB_ROOT_PATH . 'includes/acp/auth.' . PHP_EXT);
			}
			$auth_admin = new auth_admin();
			$auth_admin->acl_add_option(array(
				'local'		=> array(),
				'global'	=> array(
					// Admin stuffs
					'a_crs_add_project',
					'a_crs_edit_project',
					'a_crs_project_settings',
					
					// Global permission for Code Repository (Without it, no access at all)
					'u_crs_project_use',
					
					// Main project system stuff
					'u_crs_project_main_list',
					'u_crs_project_main_view',
					'u_crs_project_subscribe',
					
					// Project extensions
					// -- Project History
					'u_crs_project_history_view',
					'u_crs_project_history_download',
					// -- Project Code Repository
					'u_crs_project_coderepo_view',
					'u_crs_project_coderepo_viewfile',
					'u_crs_project_coderepo_viewimage',
					'u_crs_project_coderepo_highlight',
					// -- Project Comments
					'u_crs_project_comments_view',
					'u_crs_project_comments_make',
					'u_crs_project_comments_report',
					'u_crs_project_comments_edit',
					'u_crs_project_comments_delete',
					// These are user-level permissions, but are intended for moderators.
					// We don't want project members getting MCP access just to moderate the comments pages, 
					// since there's not going to be an MCP module for that, you know.  ;)
					'u_crs_project_comments_m_edit',
					'u_crs_project_comments_m_delete',
					'u_crs_project_comments_a_edit',
					'u_crs_project_comments_a_delete',

					// -- Project Download
					'u_crs_project_download',
					// -- Project Live Package Generator
					'u_crs_project_livepack',
			
					// Not yet... ;)
					// -- Project Bug Tracker
					//'u_crs_project_tracker_view',
					//'u_crs_project_tracker_make',
					//'u_crs_project_tracker_reply',
					// -- Project Tasks
					//'u_crs_project_tasks_view',
				),
			));
		}

		
		/**
		* EAMI integration for lazy people.  XD
		* @todo: Update for new batch of ACP modules.
		
		$eami = new eami();

		// Add our ACP module and a parent module...
		// Parent module...
		$module_data = array(
			'module_langname'	=> 'ACP_MODS_INSTALLED',
		);
		$eami->add_module('acp', 'ACP_CAT_DOT_MODS', $module_data);		
		
		// Main module...
		$sql_ary = array(
			'module_basename'	=> 'crs_main',
			'module_langname'	=> 'ACP_CRS_MAIN',
			'module_mode'		=> 'default',
			'module_auth'		=> 'acl_a_server');
		$eami->add_module('acp', 'ACP_MODS_INSTALLED', $sql_ary);
		*/

		$this->page_title = $user->lang['STAGE_DATABASE'];

		$template->assign_vars(array(
			'TITLE'			=> $user->lang['STAGE_DATABASE'],
			'BODY'			=> $user->lang['INSTALL_DATABASE_BODY'],
			'L_SUBMIT'		=> $user->lang['CRS_NEXT_STEP'],
			'U_ACTION'		=> append_sid($this->p_master->module_url . "?mode=$mode&amp;sub=final"),
		));		
	}

	private function crs_install_main()
	{
		global $user, $template, $mode;

		$this->page_title = $user->lang['STAGE_MAIN'];

		if (!request_var('install_crs', 0))
		{
			$template->assign_vars(array(
				'TITLE'					=> $user->lang['STAGE_MAIN'],
				'BODY'					=> $user->lang['INSTALL_INTRO_BODY'],
				'AGREE_INSTALL_CRS'		=> sprintf($user->lang['AGREE_INSTALL_CRS'], CRS_VERSION),

				'L_SUBMIT'				=> $user->lang['CRS_NEXT_STEP'],
				'U_ACTION'				=> append_sid($this->p_master->module_url . "?mode=$mode"),
			));
		}
		else
		{
			redirect(append_sid($this->p_master->module_url . "?mode=$mode&amp;sub=database"));	
		}
	}
}

?>